package com.yanxinim.cn.store;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.yanxinim.cn.AppConfig;
import com.yanxinim.cn.R;
import com.yanxinim.cn.store.bean.OrderInfo;
import com.yanxinim.cn.ui.base.BaseActivity;
import com.yanxinim.cn.util.ToastUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class StoreOrderDetailsActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tv_order_id)
    TextView tvOrderId;
    @BindView(R.id.tv_order_status)
    TextView tvOrderStatus;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_user_phone)
    TextView tvUserPhone;
    @BindView(R.id.tv_user_address)
    TextView tvUserAddress;
    @BindView(R.id.rl_address)
    RelativeLayout rlAddress;
    @BindView(R.id.rv_goods_data)
    RecyclerView rvGoodsData;
    @BindView(R.id.tv_goods_total)
    TextView tvGoodsTotal;
    @BindView(R.id.tv_goods_address_fee)
    TextView tvGoodsAddressFee;
    @BindView(R.id.tv_goods_discount_fee)
    TextView tvGoodsDiscountFee;
    @BindView(R.id.scroll_view)
    NestedScrollView scrollView;
    @BindView(R.id.tv_tag)
    TextView tvTag;
    @BindView(R.id.tv_order_total)
    TextView tvOrderTotal;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_right)
    TextView tvRight;
    private OrderInfo orderInfo;

    @SuppressLint("DefaultLocale")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_details);
        ButterKnife.bind(this);
        orderInfo = (OrderInfo) getIntent().getSerializableExtra("orderInfo");
        ivBack.setOnClickListener(v -> finish());
        tvOrderId.setText(String.format("订单号：%s", orderInfo.getId()));
        switch (orderInfo.getOrderState()) {//0待付款，1待发货，2已发货，3已完成，4已取消，5退款中
            case "0":
                tvOrderStatus.setText("待付款");
                tvRight.setText("去支付");
                tvCancel.setText("取消订单");
                tvRight.setVisibility(View.VISIBLE);
                tvCancel.setVisibility(View.VISIBLE);
                break;
            case "1":
                tvOrderStatus.setText("待发货");
                tvRight.setVisibility(View.GONE);
                tvCancel.setVisibility(View.VISIBLE);
                tvCancel.setText("取消订单");
                break;
            case "2":
                tvOrderStatus.setText("已发货");
                tvRight.setText("确认收货");
                tvCancel.setText("取消订单");
                tvRight.setVisibility(View.VISIBLE);
                tvCancel.setVisibility(View.VISIBLE);
                break;
            case "3":
                tvOrderStatus.setText("已完成");
                tvRight.setVisibility(View.VISIBLE);
                tvRight.setText("删除订单");
                tvCancel.setVisibility(View.GONE);
                break;
            case "4":
                tvOrderStatus.setText("已取消");
                tvRight.setVisibility(View.VISIBLE);
                tvRight.setText("删除订单");
                tvCancel.setVisibility(View.GONE);
                break;
            case "5":
                tvOrderStatus.setText("退款中");
                tvRight.setVisibility(View.GONE);
                tvCancel.setVisibility(View.GONE);
                break;
        }
        if (orderInfo.getListOrderdata() != null) {
            StoreGoodsAdapter goodsAdapter = new StoreGoodsAdapter(this, orderInfo.getListOrderdata(),2);
            goodsAdapter.setAdapterOnClickListener(new StoreGoodsAdapter.AdapterOnClickListener() {
                @Override
                public void onClick(int position, Goods data) {
                    Intent intent = new Intent(StoreOrderDetailsActivity.this, StoreGoodsDetailsActivity.class);
                    intent.putExtra("productId", data.getId());
                    startActivity(intent);
                }
            });
            rvGoodsData.setLayoutManager(new LinearLayoutManager(this));
            rvGoodsData.setAdapter(goodsAdapter);
        }
        tvUserName.setText(orderInfo.getUserName());
        tvUserPhone.setText(orderInfo.getUserPhone());
        tvUserAddress.setText(orderInfo.getUserAddress());

        tvGoodsTotal.setText(String.format("￥%.2f", Double.parseDouble(orderInfo.getOrderMoney())));
        tvGoodsAddressFee.setText(orderInfo.getOrderBill());
        tvGoodsDiscountFee.setText(orderInfo.getOrderDiscountMoney());
        tvOrderTotal.setText(String.format("￥%.2f", Double.parseDouble(orderInfo.getOrderMoney())));


        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateOrder("4");
            }
        });

        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (orderInfo.getOrderState()) {//0待付款，1待发货，2已发货，3已完成，4已取消，5退款中
                    case "0":
                        ToastUtil.showToast(StoreOrderDetailsActivity.this,"去支付");
                        break;
                    case "1":
                        break;
                    case "2":
//                        确认收货
                       updateOrder("3");
                        break;
                    case "3":
                    case "4":
                       delOrder();
                        break;
                    case "5":
                        break;
                }
            }
        });
    }

    private void delOrder() {
        Map<String, String> map = new HashMap<>();
        map.put("orderId",orderInfo.getId());
        HttpUtils.get().url(AppConfig.STORE_DELETE_ORDER)
                .params(map)
                .build()
                .execute(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        JSONObject jsonObject = JSON.parseObject(result);
                        if (jsonObject.getInteger("resultCode")==1){
                            finish();
                        }
                    }
                });

    }
    private void updateOrder(String status) {
        Map<String, String> map = new HashMap<>();
        map.put("orderId",orderInfo.getId());
        map.put("orderState",status);
        HttpUtils.get().url(AppConfig.STORE_UPDATE_ORDER)
                .params(map)
                .build()
                .execute(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        JSONObject jsonObject = JSON.parseObject(result);
                        if (jsonObject.getInteger("resultCode")==1){
                            finish();
                        }
                    }
                });

    }
}
